package com.polimi.timer.business.weather.util;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TimeMapping {	
	/**
	 * MAPPEDDATE() PERMIT TO OBTAIN A MATCH WITH DATE OF JSON OBJECT,	
	 *  BECAUSE JSON DATE IS EVERY 3H
	 */
	public static String mappedDate(Date rawDate){
		@SuppressWarnings("unused")
		String resultdate;
		Calendar cal=Calendar.getInstance();		
		cal.setTime(rawDate);
		int maphour = 0;
		int timeTominutes=cal.get(Calendar.HOUR_OF_DAY)*60+Calendar.MINUTE;
		if(timeTominutes>1350 && timeTominutes<90){maphour=0;}
		if(timeTominutes>90 && timeTominutes<270){maphour=3;}
		if(timeTominutes>270 && timeTominutes<450){maphour=9;}
		if(timeTominutes>450 && timeTominutes<630){maphour=12;}
		if(timeTominutes>630 && timeTominutes<810){maphour=15;}
		if(timeTominutes>810 && timeTominutes<990){maphour=18;}
		if(timeTominutes>990 && timeTominutes<1170){maphour=21;}
		if(timeTominutes>1170 && timeTominutes<1350){maphour=0;}		
		cal.set(Calendar.HOUR_OF_DAY, maphour);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.SECOND, 0);
		DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS");
		resultdate = dateformat.format(cal.getTime());
		return dateformat.format(cal.getTime());
	}
}
